﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace nr.GLV.Data
{
    /// <summary>
    /// Gestisce la persistenza dei dati sul database.
    /// </summary>
    public class DBO
    {
        private static nrDataSet data;

        public static nrDataSet Data { get { return GetCachedData(); } }

        private static string DataFileName
        {
            get
            {
                string path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
                return System.IO.Path.Combine(path, "nr.data");
            }
        }

        private static nrDataSet GetCachedData()
        {
            if (data == null)
                try
                {
                    //nrDataSetTableAdapters.StatesTableAdapter states = new nrDataSetTableAdapters.StatesTableAdapter();
                    //nrDataSetTableAdapters.CitiesTableAdapter cities = new nrDataSetTableAdapters.CitiesTableAdapter();
                    //nrDataSetTableAdapters.CustomersTableAdapter customers = new nrDataSetTableAdapters.CustomersTableAdapter();
                    //nrDataSetTableAdapters.GravesTableAdapter graves = new nrDataSetTableAdapters.GravesTableAdapter();
                    //nrDataSetTableAdapters.PaymentsTableAdapter payments = new nrDataSetTableAdapters.PaymentsTableAdapter();

                    data = new nrDataSet();
                    //states.Fill(data.States);
                    //cities.Fill(data.Cities);
                    //customers.Fill(data.Customers);
                    //graves.Fill(data.Graves);
                    //payments.Fill(data.Payments);
                    string dataFileName = DataFileName;
                    if (System.IO.File.Exists(dataFileName))
                        data.ReadXml(dataFileName);
                }
                catch (Exception ex)
                {
                    throw new Exceptions.FillException(string.Empty, ex);
                }
            return data;
        }

        public static void Update()
        {
            if ((data != null) && (data.HasChanges()))
                try
                {
                    //nrDataSetTableAdapters.StatesTableAdapter states = new nrDataSetTableAdapters.StatesTableAdapter();
                    //nrDataSetTableAdapters.CitiesTableAdapter cities = new nrDataSetTableAdapters.CitiesTableAdapter();
                    //nrDataSetTableAdapters.CustomersTableAdapter customers = new nrDataSetTableAdapters.CustomersTableAdapter();
                    //nrDataSetTableAdapters.GravesTableAdapter graves = new nrDataSetTableAdapters.GravesTableAdapter();
                    //nrDataSetTableAdapters.PaymentsTableAdapter payments = new nrDataSetTableAdapters.PaymentsTableAdapter();

                    //states.Update(data);
                    //cities.Update(data);
                    //customers.Update(data);
                    //graves.Update(data);
                    //payments.Update(data);
                    data.WriteXml(DataFileName);
                    data.AcceptChanges();
                }
                catch (Exception ex)
                {
                    throw new Exceptions.UpdateException(string.Empty, ex);
                }
        }

        public static void Backup(System.IO.Stream s)
        {
            Data.WriteXml(s);
        }

        public static void Restore(System.IO.Stream s)
        {
            Data.ReadXml(s);
        }
    }
}
